using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;

namespace SO.MyMoney.Features.MyMoneyFeature
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("deccf074-ccf6-41d1-8d82-810a83cf15b7")]
    public class MyMoneyFeatureEventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            try
            {
                SPWeb web = properties.Feature.Parent as SPWeb;

                SPList list = web.Lists.TryGetList(Definitions.BOOKING_LIST);
                Guid listId;
                SPListItem item;

                if (list == null)
                {
                    listId = web.Lists.Add(Definitions.BOOKING_LIST, "", SPListTemplateType.GenericList);

                    list = web.Lists[listId];

                    list.Fields["Title"].Title = Definitions.FIELD_BOOKING_TITLE;

                    list.Fields.Add(Definitions.FIELD_BOOKING_DATE, SPFieldType.DateTime, true);

                    SPFieldCurrency curField = (SPFieldCurrency)list.Fields.CreateNewField(SPFieldType.Currency.ToString(),
                        Definitions.FIELD_BOOKING_AMOUNT);
                    curField.CurrencyLocaleId = 1031;
                    curField.DisplayFormat = SPNumberFormatTypes.TwoDecimals;
                    curField.Required = true;
                    list.Fields.Add(curField);

                    list.Fields.Add(Definitions.FIELD_BOOKING_MANDATORY, SPFieldType.Integer, true);
                    list.Fields.Add(Definitions.FIELD_BOOKING_PERIOD, SPFieldType.Integer, true);
                    list.Fields.Add(Definitions.FIELD_BOOKING_ACCOUNT, SPFieldType.Integer, true);
                    list.Fields.Add(Definitions.FIELD_BOOKING_YEAR, SPFieldType.Calculated, true);
                    list.Fields.Add(Definitions.FIELD_BOOKING_MONTH, SPFieldType.Calculated, true);

                    list.Update();

                    SPFieldCalculated calField = (SPFieldCalculated)list.Fields[Definitions.FIELD_BOOKING_YEAR];
                    calField.Formula = string.Format("=YEAR([{0}])", Definitions.FIELD_BOOKING_DATE);
                    calField.Update();

                    calField = (SPFieldCalculated)list.Fields[Definitions.FIELD_BOOKING_MONTH];
                    calField.Formula = string.Format("=MONTH([{0}])", Definitions.FIELD_BOOKING_DATE);
                    calField.Update();

                    list.Update();
                }

                list = web.Lists.TryGetList(Definitions.BOOKING_ACCOUNT_LIST);
                if (list == null)
                {
                    listId = web.Lists.Add(Definitions.BOOKING_ACCOUNT_LIST, "", SPListTemplateType.GenericList);

                    list = web.Lists[listId];

                    list.Fields.Add(Definitions.FIELD_ACCOUNT_NO, SPFieldType.Integer, true);
                    list.Fields.Add(Definitions.FIELD_ACCOUNT_TYPE, SPFieldType.Choice, true);

                    list.Update();
                    
                    SPFieldChoice choiceField = (SPFieldChoice)list.Fields[Definitions.FIELD_ACCOUNT_TYPE];
                    choiceField.Choices.Add(Definitions.FIELD_ACCOUNT_VALUE_CREDIT);
                    choiceField.Choices.Add(Definitions.FIELD_ACCOUNT_VALUE_DEBIT);
                    choiceField.Update();

                    list.Update();
                }

                list = web.Lists.TryGetList(Definitions.BOOKING_PERIOD_LIST);
                if (list == null)
                {
                    listId = web.Lists.Add(Definitions.BOOKING_PERIOD_LIST, "", SPListTemplateType.GenericList);

                    list = web.Lists[listId];
                    list.Fields.Add(Definitions.FIELD_PERIOD_START, SPFieldType.DateTime, true);
                    list.Fields.Add(Definitions.FIELD_PERIOD_END, SPFieldType.DateTime, true);
                    list.Update();
                }

                list = web.Lists.TryGetList(Definitions.SETTING_LIST);
                if (list == null)
                {
                    listId = web.Lists.Add(Definitions.SETTING_LIST, "", SPListTemplateType.GenericList);

                    list = web.Lists[listId];
                    list.Fields.Add(Definitions.FIELD_SETTING_VALUE, SPFieldType.Text, true);
                    list.Update();

                    item = list.Items.Add();
                    item["Title"] = Definitions.SETTING_SYS_MANDATORY;
                    item[Definitions.FIELD_SETTING_VALUE] = "0";
                    item.Update();
                }

                list = web.Lists.TryGetList(Definitions.BOOKING_MANDATORY_LIST);
                if (list == null)
                {
                    listId = web.Lists.Add(Definitions.BOOKING_MANDATORY_LIST, "", SPListTemplateType.GenericList);

                    list = web.Lists[listId];

                    list.Fields.Add(Definitions.FIELD_MANDATORY_NUMBER, SPFieldType.Integer, true);
                    list.Fields.Add(Definitions.FIELD_MANDATORY_CURPER, SPFieldType.Integer, false);

                    list.Update();

                    item = list.Items.Add();
                    item[Definitions.FIELD_MANDATORY_NUMBER] = 0;
                    item["Title"] = "SYSTEM MANDATORY";
                    item.Update();
                }
            }
            catch (Exception ex)
            {
            }
        }


        // Uncomment the method below to handle the event raised before a feature is deactivated.

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
#if DEBUG
            
#endif
        }


        // Uncomment the method below to handle the event raised after a feature has been installed.

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised before a feature is uninstalled.

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        // Uncomment the method below to handle the event raised when a feature is upgrading.

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
    }
}
